#!/bin/bash

APP_NAME=bale.jar
PORT=8080

FILE_NAME=$(basename $0) #不用修改,当前执行脚本文件名称
colors='\033[33m'
colore='\033[0m'

usage() {
    echo -e "***使用说明***: 
     1.启动：${colors} sh ${FILE_NAME} start ${colore}
     2.停止：${colors} sh ${FILE_NAME} stop ${colore}
     3.重启：${colors} sh ${FILE_NAME} restart ${colore}
     4.状态：${colors} sh ${FILE_NAME} status ${colore}
     5.日志：${colors} sh ${FILE_NAME} log ${colore}"
    exit 1
}
is_exist(){
  pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' `
  if [ -z "${pid}" ]; then
   return 1
  else
    return 0
  fi
}
start(){
  is_exist
  if [ $? -eq "0" ]; then
    echo "${APP_NAME} 正在运行。 pid=${pid} ."
  else
    #nohup java -server -Xms2048m -Xmx2048m -Xmn680m -Xloggc:./logs/gc.log -jar $APP_NAME  > /dev/null 2>&1 &
    #START_LOG_PATH=$FILE_NAME.log
    #nohup java -server -Xms512m -Xmx512m -Xmn128m -Xloggc:./logs/gc.log -jar $APP_NAME  > $START_LOG_PATH 2>&1 &
    #echo "${APP_NAME}启动 成功，请查看日志确保运行正常。"
    #echo "打开端口："$PORT"...请重启防火墙..."
    #firewall-cmd --zone=public --add-port=$PORT/tcp --permanent
    #if [ -f $START_LOG_PATH ];then
    #   tail -f $START_LOG_PATH
    #fi
    nohup java -server -Xms512m -Xmx512m -Xmn128m -Xloggc:./logs/gc.log -jar $APP_NAME 2>&1 &
    log
  fi
}
stop(){
  is_exist
  if [ $? -eq "0" ]; then
    kill -9 $pid
    echo "${pid} 进程已被杀死，程序停止运行"
  else
    echo "${APP_NAME} 没有运行。"
  fi
}
status(){
  is_exist
  if [ $? -eq "0" ]; then
    echo "${APP_NAME} 正在运行。Pid is ${pid}"
  else
    echo "${APP_NAME} 没有运行。"
  fi
}
log(){
  sleep 1s
  if [ -f nohup.out ];then
    tail -600f nohup.out
  else
    echo "日志文件nohup.out不存在"
  fi
}
restart(){
  stop
  start
}
case "$1" in
  "start")
    start
    ;;
  "stop")
    stop
    ;;
  "status")
    status
    ;;
  "log")
    log
    ;;
  "restart")
    restart
    ;;
  *)
    usage
    ;;
esac
